#include <SpammerI.h>

class AMI_Agent_receiveMessageI : public FIPA::AMI_Agent_receiveMessage
{
public:

    virtual void ice_response()
    {
    }

    virtual void ice_exception(const Ice::Exception& ex)
    {
        cerr << "receiveMessage AMI call failed:\n" << ex << endl;
    }
};

typedef IceUtil::Handle<AMI_Agent_receiveMessageI> AMI_Agent_receiveMessageIPtr;

class AMI_MTS_receiveMessageI : public FIPA::AMI_MTS_receiveMessage
{
public:

    virtual void ice_response()
    {
    }

    virtual void ice_exception(const Ice::Exception& ex)
    {
        cerr << "receiveMessage AMI call failed:\n" << ex << endl;
    }
};

typedef IceUtil::Handle<AMI_MTS_receiveMessageI> AMI_MTS_receiveMessageIPtr;

SpammerI::SpammerI
(Ice::Identity name,
 Ice::ObjectAdapterPtr oa) :
  AgentI(name, oa)
{
}
 
SpammerI::~SpammerI()
{
}

void
SpammerI::beginTest
(bool useMTS, 
 Ice::Int messages, 
 Ice::Int messageSizeInBytes,
 const Ice::Current& curr)
{
  IceGrid::QueryPrx query;
  FIPA::DFPrx df;
  FIPA::MTSPrx mts;
  Test::SnifferPrx sniffer;
  FIPA::AgentPrx proxy;
  
  vector<FIPA::AgentPrx> users;
  FIPA::ServiceDescription sd;
  FIPA::ServiceDescriptionSeq sds;
  FIPA::DfAgentDescription desc;

  FIPA::Envelope env;
  FIPA::ACLPayload pay;
  FIPA::Message m;
  AMI_Agent_receiveMessageIPtr cba;
  AMI_MTS_receiveMessageIPtr cbm;

  int i = 0;

  query = IceGrid::QueryPrx::checkedCast
    (curr.adapter->getCommunicator()->stringToProxy("FIPA/Query"));
  df = FIPA::DFPrx::checkedCast
    (curr.adapter->getCommunicator()->stringToProxy("DF"));
  mts = FIPA::MTSPrx::checkedCast
    (curr.adapter->getCommunicator()->stringToProxy("MTS"));
  sniffer = Test::SnifferPrx::checkedCast
    (curr.adapter->getCommunicator()->stringToProxy("Sniffer"));
  proxy = FIPA::AgentPrx::uncheckedCast
    (curr.adapter->createIndirectProxy(_name));

  // Look for user agents.
  sd.name = "user";
  sds.push_back(sd);
  desc.services = sds;
  users = df->search(desc);

  // Spamming...
  sniffer->beginTest("spammer");

  env.to = users;
  //env.frm = proxy;
  
  // Initial message...
  pay.performative = FIPA::Confirm;
  pay.content = "beginning";

  // Fill the content...
  pay.ontology = "";
  for (int i = 0;
       i < messageSizeInBytes;
       ++i)
    pay.ontology += i;

  m.env = env;
  m.payload = pay;
  cba = new AMI_Agent_receiveMessageI();  
  cbm = new AMI_MTS_receiveMessageI();  

  if (useMTS)
    mts->receiveMessage_async(cbm, m);
  else
    for (vector<FIPA::AgentPrx>::const_iterator it = users.begin();
	 it != users.end();
	 ++it)
      (*it)->receiveMessage_async(cba, m);

  m.payload.content = "";
  
  while (i < messages - 1)
    {
      if (useMTS)
	mts->receiveMessage_async(cbm, m);
      else
	for (vector<FIPA::AgentPrx>::const_iterator it = users.begin();
	     it != users.end();
	     ++it)
	  (*it)->receiveMessage_async(cba, m);
      ++i;
    }

  // Final message...
  m.payload.content = "end";
  if (useMTS)
    mts->receiveMessage_async(cbm, m);
  else
    for (vector<FIPA::AgentPrx>::const_iterator it = users.begin();
	 it != users.end();
	 ++it)
      (*it)->receiveMessage_async(cba, m);

  sniffer->endTest("spammer");
}
